<?php
/*
 * Created on Feb 5, 2009
 * @author: ANH
 * @PARAM rtype is rootType: example: caves is type then attraction is rtype
 * @filesource: modules/place/controllers/PlaceController.php
 */

class Event_DevelController extends Clu_Controller_Action_Hvn
{
	protected $mode;
	public function init()
	{
		//require_once  (APP_PATH.'/modules/article/Model/Article.php');
		//require_once  (APP_PATH.'/modules/category/Model/Category.php');
		//require_once  (APP_PATH.'/modules/comment/Model/Comment.php');
		//require_once  (APP_PATH.'/modules/user/Model/User.php');
		//require_once  (APP_PATH.'/modules/place/Model/City.php');
		//require_once  (APP_PATH.'/modules/place/Model/District.php');
		//require_once  (APP_PATH.'/modules/place/Model/Village.php');
		$this->m = new event_Model_Service();
		$this->mCity = new place_Model_City();
		$this->mDistrict = new place_Model_District();
		$this->mVillage = new place_Model_Village();
		$this->mCategory = new category_Model_Category();
		$this->mCategory->setTables('event');
		$this->mode = 'debug';
		parent::init();
	}
    
    public function updateMetadataOfEventAction()
    {
    	$sql = "SELECT * FROM events";
	    $results = $this->db->fetchAll($sql);
	    //require_once(APP_PATH . "/modules/user/Model/User.php");
	    $uModel = new user_Model_User();

    	for ($i = 0; $i < 10000; $i++)
    	{
    		$p = $this->m->findByPrimary($i);
    		if (!$p)
    			continue;

	    	$metadata = unserialize($p['metadata']);
	    	
	    	if(isset($metadata['poster_name']))
	    	{
	    		$metadata['poster_name'] = $metadata['poster_name'];
	    	}
	    	else
	    	{
		    	$posterId = $p['poster_id'];
		    	$u = $uModel->findByPrimary($posterId);
		    	$metadata['poster_name'] = $u['name'];
	    	}
			
			if(isset($metadata['belong_to_ids']))
				$metadata['belong_to_ids'] = $metadata['belong_to_ids'];	

			if(isset($metadata['belong_to_names']))
				$metadata['belong_to_names'] = $metadata['belong_to_names'];
				
			if(isset($metadata['distance']))
				$metadata['distance'] = $metadata['distance'];
			
			if(isset($metadata['place_id']))
				$metadata['place_id'] = $metadata['place_id'];
					
	    	$categoryName = $this->mCategory->getCategoryName($p['category']);
	    	$metadata['categoryName'] = $categoryName;
	    	if(isset($p['city_id']))
	    	{
	        	$cityName = $this->mCity->getCityName($p['city_id']);
		        $metadata['city_name'] = $cityName['city_name'];
	    	}
	    		
	    	if(isset($p['district_id']))
	    	{
		        $districtName = $this->mDistrict->getDistrictName($p['district_id']);
		        $metadata['district_name'] = $districtName['district_name'];
	    	}
	    		
	    	if(isset($p['village_id']))
	    	{
		        $villageName = $this->mVillage->getVillageName($p['village_id']);
		        $metadata['village_name'] = $villageName['village_name'];
	    	}
	    	
	    	$p['metadata'] = serialize($metadata);
	    	try{
		    	$p->save();
		    }
		    catch(Exception $e)
		    {
		    	die_a($e);
		    	$e->getMessage();	
		    }
    	}
    	//end for. Finished looping through all places
	    die('finish');
    		  
    }
}
?>
